package cn.jiangxiong.pethome.system.dept.service.impl;

import cn.jiangxiong.pethome.base.service.impl.BaseServiceImpl;
import cn.jiangxiong.pethome.system.dept.domain.Department;
import cn.jiangxiong.pethome.system.dept.domain.DepartmentRo;
import cn.jiangxiong.pethome.system.dept.mapper.DepartmentMapper;
import cn.jiangxiong.pethome.system.dept.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Transactional(readOnly = true , propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
   @Autowired
   private DepartmentMapper departmentMapper;


   @Transactional
   @Override
   public void add(Department department){
       departmentMapper.add(department);
       buildDirPath(department);
       departmentMapper.update(department);

   }

   //抽出公共方法
    private void buildDirPath(Department department){
       if(department.getParent_id() == null){
           Long id = department.getId();
           department.setDirPath(("/"+id));

       }else {
           Long parentId = department.getParent_id();
           Department parent = departmentMapper.findOne(parentId);
           String dirPath = parent.getDirPath();
           department.setDirPath(dirPath+"/"+department.getId());
       }
    }
    @Transactional
    @Override
    public void update(Department department){
       if(department.getId().equals(department.getParent_id())){
           throw new RuntimeException("无法设置自己为自己上级部门");
       }
        buildDirPath(department);
        departmentMapper.update(department);
    }
    @Transactional
    @Override
    public void delete(Long id){
        //有下级部门时不能删除
        Long  count = departmentMapper.findByParentId(id);
        if (count == 0){
            departmentMapper.delete(id);

        }else {
            throw new RuntimeException("当前部门存在下级不能直接删除");
        }
    }

    @Override
    public List<Department> deptTree() {
       //第一步查询全部
        List<Department> departments = departmentMapper.findAll();
        //第三步建立关系网，有子类找出父类
        for (Department department:departments){
            //找出最高级
            if (department.getParent_id() !=null){
                for (Department parent : departments){
                    //判断是否为父类
                    if (department.getParent_id() == parent.getId()){
                        List<Department> children = parent.getChildren();
                        children.add(department);
                        break;
                    }
                }
            }
        }
        //第二步找到最顶级后再filer步骤中过滤出父类的id为null
        List<Department> firstTree = departments.stream()
                .filter(department -> department.getParent_id() == null)
                .collect(Collectors.toList());//转为list
        return firstTree;
    }

    @Override
    public List<DepartmentRo> countByDate() {
        return departmentMapper.countByDate();
    }
}
